package edu.vt.ece3574.ninjatag;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.util.SparseBooleanArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;


/**
 * This dialog is a not-so self-contained way to
 * advertise Ninja-Tag to friends who don't currently
 * play Ninja-Tag.
 * @author Chris Dusold
 *
 */
public class InviteNewPlayersScreen extends Activity implements OnClickListener {
    private Handler mHandler;

    protected ListView friendsList;
    protected static JSONArray jsonArray;
    protected AppInviteListAdapter adapter;
	
	private Button sendInvite, cancel;

	@Override
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.invite_new_screen);
        
        cancel = (Button)findViewById(R.id.backtoMatchmakingButton);
        sendInvite = (Button)findViewById(R.id.sendInvitesButton);
        
        cancel.setOnClickListener(this);
        sendInvite.setOnClickListener(this);

        mHandler = new Handler();
        Bundle extras = getIntent().getExtras();
        String apiResponse = extras.getString("API_RESPONSE");
        try {
            jsonArray = new JSONArray(apiResponse);
        } catch (JSONException e) {
            showToast("Error: " + e.getMessage());
            return;
        }
        friendsList = (ListView) findViewById(R.id.friends_list);
        friendsList.setItemsCanFocus(false);
        friendsList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        adapter = new AppInviteListAdapter(this, jsonArray);
        friendsList.setAdapter(adapter);
	}

	private void showToast(final String msg) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                Toast toast = Toast.makeText(InviteNewPlayersScreen.this, msg, Toast.LENGTH_LONG);
                toast.show();
            }
        });
    }
    
	@Override
	public void onClick(View v) {
		
		Intent invitesSentScreenIntent = new Intent(InviteNewPlayersScreen.this, invites_sent_screen.class);
		Intent matchmakingScreenIntent = new Intent(InviteNewPlayersScreen.this, matchmaking_screen.class);
		
		switch(v.getId()){
		
		case R.id.backtoMatchmakingButton:
			startActivity(matchmakingScreenIntent);
			finish();
			break;
			
		case R.id.sendInvitesButton:
			SparseBooleanArray checkedItems = adapter.getCheckedItemPositions(friendsList);
			if (checkedItems == null) {
				Log.e(this.getClass().getName(), "ListView returned null for checked items.");
			} else {
				final int checkedItemsCount = checkedItems.size();
				Log.d("Checked Items Size",String.valueOf(checkedItemsCount));
				for (int i = 0; i < checkedItemsCount; ++i) {
					// This tells us the item position we are looking at
					final int position = checkedItems.keyAt(i);
	
					// And this tells us the item status at the above position
					final boolean isChecked = checkedItems.valueAt(i);
	
					// And we can get our data from the adapter like that
					final JSONObject currentItem = (JSONObject) adapter.getItem(position);
					
					if (isChecked) {
						try {
							Log.d("Checkec", currentItem.getString("name"));
						} catch (JSONException e) {
							Log.e("Checked","Item without gettable name");
							e.printStackTrace();
						}
					} else {
						try {
							Log.d("Uncheckec", currentItem.getString("name"));
						} catch (JSONException e) {
							Log.e("Unchecked","Item without gettable name");
							e.printStackTrace();
						}
					}
				}
			}
			
			startActivity(invitesSentScreenIntent);
			finish();
			break;
		}
	}

}
